{% extends "base.html" %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>订单管理</h2>
        <a href="{{ url_for('order.order_add') }}" class="btn btn-primary">
            <i class="fas fa-plus"></i> 创建订单
        </a>
    </div>

    <!-- 搜索表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">订单号</label>
                    <input type="text" class="form-control" name="order_number" value="{{ order_number }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">客户名称</label>
                    <input type="text" class="form-control" name="customer_name" value="{{ customer_name }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">订单类型</label>
                    <select class="form-select" name="order_type">
                        <option value="">所有类型</option>
                        <option value="sample" {% if order_type == 'sample' %}selected{% endif %}>样本采集</option>
                        <option value="dna" {% if order_type == 'dna' %}selected{% endif %}>DNA检测</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">状态</label>
                    <select class="form-select" name="status">
                        <option value="">所有状态</option>
                        <option value="pending" {% if status == 'pending' %}selected{% endif %}>待处理</option>
                        <option value="processing" {% if status == 'processing' %}selected{% endif %}>处理中</option>
                        <option value="completed" {% if status == 'completed' %}selected{% endif %}>已完成</option>
                        <option value="cancelled" {% if status == 'cancelled' %}selected{% endif %}>已取消</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">&nbsp;</label>
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>订单号</th>
                            <th>客户名称</th>
                            <th>订单类型</th>
                            <th>状态</th>
                            <th>金额</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for order in orders %}
                        <tr>
                            <td>{{ order.order_number }}</td>
                            <td>{{ order.customer_name }}</td>
                            <td>
                                {% if order.order_type == 'sample' %}样本采集
                                {% elif order.order_type == 'dna' %}DNA检测
                                {% else %}{{ order.order_type }}{% endif %}
                            </td>
                            <td>
                                {% if order.status == 'pending' %}
                                    <span class="badge bg-warning">待处理</span>
                                {% elif order.status == 'processing' %}
                                    <span class="badge bg-info">处理中</span>
                                {% elif order.status == 'completed' %}
                                    <span class="badge bg-success">已完成</span>
                                {% elif order.status == 'cancelled' %}
                                    <span class="badge bg-danger">已取消</span>
                                {% endif %}
                            </td>
                            <td>¥{{ order.total_amount }}</td>
                            <td>{{ order.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                            <td>
                                <a href="{{ url_for('order.order_edit', id=order.id) }}" class="btn btn-sm btn-info">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                <a href="{{ url_for('order.order_delete', id=order.id) }}" class="btn btn-sm btn-danger"
                                   onclick="return confirm('确定要删除该订单吗？')">
                                    <i class="fas fa-trash"></i> 删除
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if pagination and pagination.pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('order.order_list', page=pagination.prev_num, order_number=order_number, customer_name=customer_name, order_type=order_type, status=status) if pagination.has_prev else '#' }}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                    {% for page in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                        {% if page %}
                            <li class="page-item {% if page == pagination.page %}active{% endif %}">
                                <a class="page-link" href="{{ url_for('order.order_list', page=page, order_number=order_number, customer_name=customer_name, order_type=order_type, status=status) }}">
                                    {{ page }}
                                </a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                        {% endif %}
                    {% endfor %}
                    <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('order.order_list', page=pagination.next_num, order_number=order_number, customer_name=customer_name, order_type=order_type, status=status) if pagination.has_next else '#' }}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                </ul>
            </nav>
            <div class="text-center text-muted">
                共 {{ pagination.total }} 条记录，当前第 {{ pagination.page }}/{{ pagination.pages }} 页
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %} 